//
//  EQAnalysisVC.swift
//  EQQuestionBank
//
//  Created by Macx on 2018/8/4.
//  Copyright © 2018年 FanWeiJian. All rights reserved.
//

import UIKit
import RxSwift
import WebKit

class EQAnalysisVC: UIViewController, EQNavigationBarProtocol {
    
    var model : EQQuestionModel!
    
    var analysisModel : EQAnalysisModel! = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setNavigationBar()
        setUI()
        loadData()
    }
    
    func loadData(){
        EQHUD.showWait()
        eqProvider.rx.request(EQService.problemInfo(id: model.id, type: model.type))
            .filterSuccessfulStatusAndRedirectCodes()
            .subscribeOn(MainScheduler.instance)
            .mapModel(EQResponseModel<EQAnalysisModel>.self)
            .subscribe(onSuccess: { (responseModel) in
                print(responseModel)
                self.analysisModel = responseModel.data
                self.contentWebView.loadHTMLString(self.analysisModel.content, baseURL: nil)
                self.solutionWebView.loadHTMLString(self.analysisModel.solution, baseURL: nil)
                EQHUD.hidHUD()
            }, onError: { (error) in
                EQHUD.hidHUD()
                EQHUD.showFail("未搜到题")
            }).disposed(by: self.rx.disposeBag)
    }
    
    func setNavigationBar(){
        setNavigationTitle(title: "题目解析", titleColor: .darkText)
        setBackButton()
    }
    
    func setUI(){
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(64)
            make.width.equalTo(kScreenW)
            make.bottom.left.equalToSuperview()
        }
        
        let wrapper = UIView()
        scrollView.addSubview(wrapper)
        wrapper.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(kScreenW)
        }
        
        let lable1 = UILabel()
        lable1.text = "题目"
        lable1.textColor = UIColor.eq.hex(0x5d99f0)
        wrapper.addSubview(lable1)
        lable1.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.equalTo(20)
        }
        wrapper.addSubview(contentWebView)
        contentWebView.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.width.equalTo(kScreenW - 40)
            make.top.equalTo(lable1.snp.bottom).offset(10)
            make.height.equalTo(10)
        }
        
        let lable2 = UILabel()
        lable2.text = "答案解析"
        lable2.textColor = UIColor.eq.hex(0x5d99f0)
        wrapper.addSubview(lable2)
        lable2.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.top.equalTo(contentWebView.snp.bottom).offset(20)
        }
        
        wrapper.addSubview(solutionWebView)
        solutionWebView.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.width.equalTo(kScreenW - 40)
            make.top.equalTo(lable2.snp.bottom).offset(10)
            make.height.equalTo(10)
            make.bottom.equalToSuperview()
        }
        
    }
    
    lazy var contentWebView: WKWebView = {
        let wb = WKWebView()
        wb.navigationDelegate = self
        wb.scrollView.bounces = false
        return wb
    }()
    
    lazy var solutionWebView: WKWebView = {
        let wb = WKWebView()
        wb.scrollView.bounces = false
        wb.navigationDelegate = self
        return wb
    }()
    
    lazy var scrollView: UIScrollView = {
        let sc = UIScrollView()
        self.view.addSubview(sc)
        return sc
    }()
}

extension EQAnalysisVC  : WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '250%'", completionHandler: nil)
        
        webView.evaluateJavaScript("document.body.offsetHeight") {[weak self] (resule, error) in
            if error == nil {
                print(resule!)
                let height = resule as! CGFloat
                
                if webView.isEqual(self!.contentWebView){
                    self!.contentWebView.snp.updateConstraints { (make) in
                        make.height.equalTo(height)
                    }
                }else if webView.isEqual(self!.solutionWebView){
                    self!.solutionWebView.snp.updateConstraints { (make) in
                        make.height.equalTo(height)
                    }
                }

            }
        }
    }
}



